package com.atguigu.staticservice.service.impl;

import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.ResultCode;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.staticservice.entity.StatisticsDaily;
import com.atguigu.staticservice.feign.UserService;
import com.atguigu.staticservice.mapper.StatisticsDailyMapper;
import com.atguigu.staticservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-10-11
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Resource
    private UserService userService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void registerCount(String day) {
        R r = userService.countRegister(day);
        Boolean success = r.getSuccess();
        if(!success){
           throw new GuLiException(ResultCode.ERROR,"统计失败");
        }
        // 删除重复统计的数据
        LambdaQueryWrapper<StatisticsDaily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsDaily::getDateCalculated,day);
        this.remove(queryWrapper);

        // 注册人数
        Integer count = (Integer)r.getData().get("info");
        // 登录人数 TODO
        Integer loginNum = RandomUtils.nextInt(100, 200);
        // 视屏播放数 TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);
        // 每日新增课程数 TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        // 统计日期
        statisticsDaily.setDateCalculated(day);
        // 注册人数统计
        statisticsDaily.setRegisterNum(count);
        // 登录人数
        statisticsDaily.setLoginNum(loginNum);
        // 视屏播放数
        statisticsDaily.setVideoViewNum(videoViewNum);
        // 每日新增课程数
        statisticsDaily.setCourseNum(courseNum);
        this.save(statisticsDaily);
    }

    @Override
    public Map showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .between("date_calculated",begin,end)
                .select("date_calculated", type);
        List<StatisticsDaily> list = this.list(queryWrapper);

        // 横坐标 date_calculated
        List<String> dateCalculatedList = new ArrayList<>();
        // 纵坐标 number
        List<Integer> numDateList = new ArrayList<>();

        for (StatisticsDaily statisticsDaily : list) {
            dateCalculatedList.add( statisticsDaily.getDateCalculated());
            switch (type)   {
                case "login_num":
                    numDateList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDateList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDateList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDateList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        Map map = new HashMap<>(2);
        map.put("x",dateCalculatedList);
        map.put("y",numDateList);
        return map;
    }
}
